from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall.settings import SECRET_KEY


def generic_openid(openid):
    # secret_key,  密钥
    # expires_in = None 过期时间
    # 1. 创建实例对象
    s = Serializer(secret_key=SECRET_KEY, expires_in=3600)

    # 2、组织数据
    data = {
        'openid': openid
    }
    # secret_data 是二进制，返回需要解码
    secret_data = s.dumps(data)
    # bytes --> str
    return secret_data.decode()


from itsdangerous import BadSignature


def check_token(serect_data):
    s = Serializer(secret_key=SECRET_KEY, expires_in=3600)

    try:
        data = s.loads(serect_data)
    except BadSignature:
        return None

    return data.get('openid')
